﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class ShangJiaIsReadZhongXinGongGaoDAO : Repository<Model.Entities.Model_ShangJiaIsReadZhongXinTongGao>, Chinaer.IDao.IShangJiaIsReadZhongXinGongGaoDAO
    {
        /// <summary>
        /// 根据中心公告编号和商家编号 判断该商家是否已经阅读
        /// </summary>
        /// <param name="zhongXinGongGaoID"></param>
        /// <param name="shangJiaID"></param>
        /// <returns></returns>
        public bool GetShangJiaIsReadStatue(string zhongXinGongGaoID, string shangJiaID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.ShangJiaReadZhongXinGongGaos.FirstOrDefault(s => s.ShangJiaID == shangJiaID && s.ZhongXinGongGaoID == zhongXinGongGaoID && s.IsUse == true && s.IsDelete == false) == null ? false : true;
            }
        }

        public override bool Add(Model.Entities.Model_ShangJiaIsReadZhongXinTongGao entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_ShangJiaIsReadZhongXinTongGao entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            entity.IsUse = false;
            return base.Update(entity);
            //return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            Model.Entities.Model_ShangJiaIsReadZhongXinTongGao shangjiaIsRead = GetByID(id);
            return Delete(shangjiaIsRead);
        }

        public override Model.Entities.Model_ShangJiaIsReadZhongXinTongGao GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.ShangJiaReadZhongXinGongGaos.Where(p => p.IsUse == true && p.IsDelete == false && p.ID.Equals(id)).FirstOrDefault();
            }
        }

        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from s in context.ShangJiaReadZhongXinGongGaos
                                  where listID.Contains(s.ID)
                                  select s;
                foreach (var item in queryResult)
                {
                    context.Entry<Model.Entities.Model_ShangJiaIsReadZhongXinTongGao>(item).State = System.Data.Entity.EntityState.Modified;
                    item.IsDelete = true;
                    item.IsUse = false;
                }
                return context.SaveChanges();
            }
        }

        public override bool Exists(string id)
        {
            return GetByID(id) == null ? false : true;
        }
        public override IList<Model.Entities.Model_ShangJiaIsReadZhongXinTongGao> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_ShangJiaIsReadZhongXinTongGao entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }


    }
}
